<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <div class="left">
            <i class="el-icon-s-unfold" />
            <span style="font-family:'微软雅黑';font-weight:400;font-size:18px;">参数列表</span>
          </div>
          <div class="right">
            <el-button size="small" @click="Return" style="font: 15px">返回</el-button>
            <el-button size="small" @click="AddParam" style="font: 15px">添加</el-button>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-table :data="formatData" border style="width: 100%; text-align: center">
      <el-table-column type="selection" width="60"></el-table-column>
      <el-table-column fixed prop="id" label="编号" width="150" text-align: center></el-table-column>
      <el-table-column prop="name" label="参数名称" width="150"></el-table-column>
      <el-table-column prop="templateName" label="模版" width="120"></el-table-column>
      <el-table-column prop="options" label="可选值列表" width="200"></el-table-column>
      <el-table-column prop="seq" label="排序" width="150"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="UpdateFormat(scope.row.id)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteRow(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="total"
      layout="total, sizes,prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>
<script>
import paraApi from "@/api/goods/para";
import templateApi from "@/api/goods/template";
export default {
  data() {
    return {
      total: 0, // 总记录数
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示10条数据,
      formatData: []
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 当每页显示条数改变后,被触发 , val是最新的每页显示条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    // 当页码改变后,被触发 , val 是最新的页面
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    fetchData() {
      paraApi.pageInfo(this.currentPage, this.pageSize).then(response => {
        const resp = response.data;
        this.formatData = resp.data.list;
        this.total = resp.data.totalRows;
      });
    },

    Return() {
      // this.$router.go(-1);
      this.$router.push({
        path: "/goods/goodsSP"
      });
    },
    //删除
    deleteRow(id) {
      this.$confirm("确认删除这条记录吗？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消"
      })
        .then(() => {
          // 确认
          console.log("确认");
          paraApi.deleteById(id).then(response => {
            const resp = response.data;
            this.$message({
              message: resp.msg,
              type: resp.code == 0 ? "success" : "error"
            });
            if (resp.code == 0) {
              // 删除成功，刷新列表数据
              this.fetchData();
            }
          });
        })
        .catch(() => {
          // 取消，不用理会
          console.log("取消");
        });
    },
    UpdateFormat(id) {
      this.$router.push({
        path: "/goods/addOrUpdateParmar",
        query: { id: id }
      });
    },
    AddParam() {
      this.$router.push({
        path: "/goods/addOrUpdateParmar"
      });
    }
  }
};
</script>
<style>
.el-table .cell,
.el-table--border td:first-child .cell,
.el-table--border th:first-child .cell {
  text-align: center;
}
.el-pagination {
  float: right;
}
a {
  color: black;
  text-decoration: none;
}
a:hover {
  color: blue;
}
.el-row {
  margin-bottom: 20px;
  width: 100%;
}
.el-col {
  border-radius: 4px;
  height: 40px;
}
.bg-purple-dark {
  background: #a5b1c2;
}
.left {
  float: left;
}
.right {
  float: right;
}
.pagination-container {
  background: #fff;
  padding: 32px 16px;
}
</style>
